﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Text;
using MM.DBUtility;

namespace MobileManager
{
    public partial class UserManager : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                GVDataBind();
                divAdd.Visible = false;
                divGV.Visible = true;
                if (userModel != null && userModel.RoleID.Equals("1"))
                {
                    btnADD.Visible = true;
                }
                else btnADD.Visible = false;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string strUserID = txtUserID.Text.Trim();
                if (string.IsNullOrEmpty(strUserID))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "用户名不能为空！");
                    return;
                }

                MM.DAL.T_Users userDAL = new MM.DAL.T_Users();
                if (userDAL.Exists(strUserID))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "用户名已经存在，请更换！");
                    return;
                }

                string strPD1 = txtNewPassWord1.Text.Trim(), strPD2 = txtNewPassWord2.Text.Trim();
                if (string.IsNullOrEmpty(strPD1) || string.IsNullOrEmpty(strPD2))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "密码不能为空！");
                    return;
                }
                else if (!strPD1.Equals(strPD2))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "两次密码输入不一致！");
                    return;
                }
                string strUserName = txtUserName.Text.Trim();
                if (string.IsNullOrEmpty(strUserName))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "姓名不能为空！");
                    return;
                }
                string strRoleID = ddlRole.SelectedValue;
                string strTelephone = txtTelephone.Text.Trim();
                string strMobile = txtMobile.Text.Trim();
                string strAddress = txtAddress.Text.Trim();
                string strRemark = txtRemark.Text.Trim();
                string strDataRate = txtDataRate.Text.Trim();

                string strCustomerNO = txtCustomerNO.Text.Trim();
                string strChanelNO = txtChanelNO.Text.Trim();


                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_USERS(");
                strSql.Append("UserID,RoleID,PassWordinfo,UserName,Telephone,Mobile,Address,Remark,DataRate,CustomerNO,ChanelNO)");
                strSql.Append(" values (");
                strSql.Append(string.Format("'{0}',{1},'{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", strUserID, strRoleID, MM.Common.DESEncrypt.Encrypt(strPD1), strUserName, strTelephone, strMobile, strAddress, strRemark, strDataRate, strCustomerNO, strChanelNO));
                MM.DBUtility.DbHelperSql.ExecuteSql(strSql.ToString());
                MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                GVDataBind();
                divAdd.Visible = false;
                divGV.Visible = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnADD_Click(object sender, EventArgs e)
        {
            divAdd.Visible = true;
            divGV.Visible = false;
            ClearData();
            if (!userModel.RoleID.Equals("1"))
                ddlRole.Enabled = false;
        }
        private void ClearData()
        {
            txtUserID.Text = "";
            txtNewPassWord1.Text = "";
            txtNewPassWord2.Text = "";
            txtUserName.Text = "";
            txtTelephone.Text = "";
            txtMobile.Text = "";
            txtAddress.Text = "";
            txtRemark.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GVDataBind();
            divAdd.Visible = false;
            divGV.Visible = true;
        }

        #region GV function
        private void GVDataBind()
        {
            DataSet ds = DbHelperSql.Query("SELECT * FROM T_Users");
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
        }
        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!(userModel != null && userModel.RoleID.Equals("1")))
            {
                if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[1].Visible = false;//密码
                    e.Row.Cells[3].Visible = false;//角色
                    e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;//管理
                    e.Row.Cells[e.Row.Cells.Count - 2].Visible = false;//通道号码
                    e.Row.Cells[e.Row.Cells.Count - 3].Visible = false;//客户编号
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                string strRoleID = ((DataRowView)e.Row.DataItem)["RoleID"].ToString().ToLower();
                Label lbRole = (Label)e.Row.FindControl("lbRole");
                object O = MM.DBUtility.DbHelperSql.GetSingle(string.Format("SELECT RoleName FROM  T_Roles WHERE RoleID={0}", strRoleID));
                if (O != null)
                {
                    lbRole.Text = O.ToString();
                }

                Label lbPD = (Label)e.Row.FindControl("lbPD");
                if (lbPD != null)
                    lbPD.Text = MM.Common.DESEncrypt.Decrypt(((DataRowView)e.Row.DataItem)["PassWordinfo"].ToString());
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVDataBind();
                string strRoleID = ((HiddenField)GVinfo.Rows[e.NewEditIndex].FindControl("hdfRoleID")).Value.Trim();
                DropDownList ddlStatus = ((DropDownList)GVinfo.Rows[e.NewEditIndex].FindControl("ddlRole"));
                ddlRole.SelectedValue = strRoleID;

                string strPD = ((HiddenField)GVinfo.Rows[e.NewEditIndex].FindControl("hdfPD")).Value;
                TextBox txtPD = ((TextBox)GVinfo.Rows[e.NewEditIndex].FindControl("txtPD"));
                if (txtPD != null)
                    txtPD.Text = MM.Common.DESEncrypt.Decrypt(strPD);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                string strUserID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("hdfUserID")).Value;
                string strRoleID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("hdfRoleID")).Value;
                if (strRoleID.Equals("3"))
                {
                    ArrayList alSQL = new ArrayList();
                    alSQL.Add(string.Format("DELETE FROM T_User_Customer WHERE UserID='{0}'", strUserID));
                    alSQL.Add(string.Format("DELETE FROM T_Users WHERE ID={0}", strID));

                    DbHelperSql.ExecuteSqlTran(alSQL);
                }
                else DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_Users WHERE ID={0}", strID));
                GVDataBind();
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strPD = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtPD")).Text.Trim();
                if (string.IsNullOrEmpty(strPD))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "密码不能为空！");
                    return;
                }
                string strUserName = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtUserName")).Text.Trim();
                if (string.IsNullOrEmpty(strUserName))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "姓名不能为空！");
                    return;
                }
                string strRoleID = ((DropDownList)GVinfo.Rows[e.RowIndex].FindControl("ddlRole")).SelectedValue;
                //string strTelephone = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtTelephone")).Text.Trim();
                string strMobile = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtMobile")).Text.Trim();
                //string strAddress = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtAddress")).Text.Trim();
                //string strRemark = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtRemark")).Text.Trim();
                //string strDataRate = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtDataRate")).Text.Trim();
                string strDataRate = string.Empty;
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;
                string strUserID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("hdfUserID")).Value;

                string strCustomerNO = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtCustomerNO")).Text.Trim();
                string strChanelNO = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtChanelNO")).Text.Trim();

                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_Users SET PassWordinfo='{0}',UserName='{1}',Mobile='{2}',RoleID={3},DataRate='{4}',CustomerNO='{5}',ChanelNO='{6}' WHERE ID={7}", MM.Common.DESEncrypt.Encrypt(strPD), strUserName, strMobile, strRoleID, strDataRate, strCustomerNO, strChanelNO, strID));
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");

                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
